using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Srvtools;

namespace cPpoPr1
{
    public partial class Form1 : InfoForm
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void infoDataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
        ////////////
        public string GetToday()
        {
            return string.Format("{0:yyyyMMdd}", DateTime.Today);
        }

        public string GetNowTime()
        {
            return string.Format("{0:hhmmss}", DateTime.Now);
        }

        public string GetUser()
        {
            return CliUtils.fLoginUser;
        }
        /////////////

        private void ibsMaster_BeforeEdit(object sender, EventArgs e)
        {                              
            //dateTime顯示
            infoTextBox6.Text = DateTime.Now.ToLongTimeString();
                     
        }

        private void ibsMaster_CurrentChanged(object sender, EventArgs e)
        {
          string state = infoNavigator1.GetCurrentState();
          if (state == "Inserting" || state == "Editing" || state == "Initial")
          {
              //修改時間顯示
              string UPDATE_TIME = ((DataRowView)ibsMaster.Current)["UPDATE_TIME"].ToString();
              if (UPDATE_TIME.Length == 6)
               infoTextBox6.Text = Convert.ToDateTime(UPDATE_TIME.Substring(0, 2) + ":" + UPDATE_TIME.Substring(2, 2) + ":" + UPDATE_TIME.Substring(4, 2)).ToLongTimeString(); 
              else if (UPDATE_TIME.Length == 0)
                  infoTextBox6.Text = "";

              //建檔時間顯示
              string CREATE_TIME = ((DataRowView)ibsMaster.Current)["CREATE_TIME"].ToString();
              if (CREATE_TIME.Length == 6)
               infoTextBox1.Text = Convert.ToDateTime(CREATE_TIME.Substring(0, 2) + ":" + CREATE_TIME.Substring(2, 2) + ":" + CREATE_TIME.Substring(4, 2)).ToLongTimeString(); 
              else if (CREATE_TIME.Length == 0)
                  infoTextBox1.Text = "";
          }
        }

        private void InfoComboBox0_TextChanged(object sender, EventArgs e)
        {
           //如果為一般性選購單,不可變更INQ,案源性質,客戶編號,業務員
            if (InfoComboBox0.Text == "一般請購單"||InfoComboBox0.Text=="1")
            {
                InfoRefValBox0.Enabled = true;  //訂單號碼
                InfoRefValBox02.Enabled = false;  //INQ
                InfoComboBox01.Enabled = false;  //案源性質
                InfoRefValBox03.Enabled = false;   //客戶編號
                InfoRefValBox01.Enabled = false;   //業務員
                this.nOITEMDataGridViewTextBoxColumn.ReadOnly = true;  //明細NO_ITEM不可改
                this.nOITEMDataGridViewTextBoxColumn.DefaultCellStyle.BackColor = System.Drawing.Color.LightGray;//明細NO_ITEM背景變灰                           
               
            }
            else if (InfoComboBox0.Text == "計劃性請購單"||InfoComboBox0.Text=="2")
            {
                InfoRefValBox0.Enabled = false;                
                InfoRefValBox02.Enabled = true;
                InfoComboBox01.Enabled = true;
                InfoRefValBox03.Enabled = true;
                InfoRefValBox01.Enabled = true;
                InfoRefValBox0.TextBoxText = "";
                infoDataGridView1.AllowUserToAddRows = true;
                this.nOITEMDataGridViewTextBoxColumn.ReadOnly = false;
                this.nOITEMDataGridViewTextBoxColumn.DefaultCellStyle.BackColor = System.Drawing.Color.White;              
            }
        }

        private void ibsMaster_AddingNew(object sender, AddingNewEventArgs e)
        {
            //在新增就顯示建檔時間   
            infoTextBox1.Text = DateTime.Now.ToLongTimeString();

            //一般請購單,設定條件,在新增時
            if (InfoComboBox0.Text == "一般請購單")
            {
                InfoRefValBox0.Enabled = true;

                InfoRefValBox02.Enabled = false;
                InfoComboBox01.Enabled = false;
                InfoRefValBox03.Enabled = false;
                InfoRefValBox01.Enabled = false;
             
                
            }
        }

        private void InfoRefValBox0_Leave(object sender, EventArgs e)
        {
            //在選定Refval之後帶入多筆明細 
            string stat = infoNavigator1.GetCurrentState();
            if (stat == "Inserting" || stat == "Editing")
            {
                ((DataRowView)ibsMaster.Current).EndEdit(); //使明細帶入的時候不會出現Key值錯誤                   
                dsORD_D.SetWhere("NO_ORDER = '" + InfoRefValBox0.TextBoxSelectedValue + "'");

                //清除明細內容
                int n = ibsDetail.List.Count;
                for (int i = 0; i < n; i++)
                {
                    ibsDetail.List.RemoveAt(0);
                }
                //將所要的明細插入  
                DataRow dr = null;
                double QT_QTY = 0, QT_PR_END = 0, PR_SALES = 0, AM_SALES = 0;

                for (int i = 0; i < dsORD_D.RealDataSet.Tables[0].Rows.Count; i++)
                {
                    QT_QTY = Convert.ToDouble(dsORD_D.RealDataSet.Tables[0].Rows[i]["QT_QTY"].ToString());
                    QT_PR_END = Convert.ToDouble(dsORD_D.RealDataSet.Tables[0].Rows[i]["QT_PR_END"].ToString());

                    //每筆重算總金額
                    PR_SALES = Convert.ToDouble(dsORD_D.RealDataSet.Tables[0].Rows[i]["PR_SALES"].ToString());
                    AM_SALES = (QT_QTY - QT_PR_END) * PR_SALES;
                    //如果數量為0,不帶出明細
                    if (QT_QTY - QT_PR_END <= 0)
                    { }
                    else
                    {
                        dr = Master.RealDataSet.Tables[1].NewRow();
                        dr["NO_PR"] = InfoTextBox0.Text;                       
                        dr["CN_SEQ"] = dsORD_D.RealDataSet.Tables[0].Rows[i]["CN_SEQ"].ToString();
                        dr["CD_IMUM"] = dsORD_D.RealDataSet.Tables[0].Rows[i]["CD_IMUM"].ToString();
                        dr["PR_SALES"] = dsORD_D.RealDataSet.Tables[0].Rows[i]["PR_SALES"].ToString();
                        dr["AM_SALES"] = AM_SALES;
                        dr["NO_ORDER"] = InfoRefValBox0.TextBoxText;
                        dr["NO_ITEM"] = dsORD_D.RealDataSet.Tables[0].Rows[i]["NO_ITEM"].ToString();
                        dr["NM_ITEM"] = dsORD_D.RealDataSet.Tables[0].Rows[i]["NM_ITEM"].ToString();
                        dr["ID_VEND"] = dsORD_D.RealDataSet.Tables[0].Rows[i]["ID_VEND"].ToString();
                        dr["FG_COUNT"] = dsORD_D.RealDataSet.Tables[0].Rows[i]["FG_COUNT"].ToString();   
                        dr["QT_QTY"] = Convert.ToString(QT_QTY - QT_PR_END);
                        dr["QT_QTY_TEMP"] = Convert.ToString(QT_QTY - QT_PR_END);
                        dr["QT_PO_END"] = "0";
                        dr["FG_CLOSE"] = "N";
                        dr["ID_PUR"] = dsORD_D.RealDataSet.Tables[0].Rows[i]["ID_PUR"].ToString();
                        Master.RealDataSet.Tables[1].Rows.Add(dr);
                    }
                    
                }
                if (ibsDetail.List.Count == 0)
                { System.Windows.Forms.MessageBox.Show("此訂單明細已請購完畢!"); }

                double AM_TOTAL;
                AM_TOTAL = Convert.ToDouble(infoDataGridView1.DoSum(8, totalMode.sum));
                InfoTextBox03.Text = AM_TOTAL.ToString();
                                
            }

            else if ((stat != "Inserting") && (stat != "Editing"))
            { return; }
        }

        private void infoDataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
             
            if ((infoNavigator1.GetCurrentState() == "Inserting") || (infoNavigator1.GetCurrentState() == "Editing"))
            {
                float AM_AMT = 0, QT_QTY = 0, QT_QTY_TEMP;
                string Qty = infoDataGridView1.Columns[e.ColumnIndex].DataPropertyName.ToString();
                if (Qty == "QT_QTY")  
                    {
                        //QT_QTY不能大於QT_QTY_TEMP
                        QT_QTY = float.Parse(((DataRowView)ibsDetail.Current)["QT_QTY"].ToString());
                        QT_QTY_TEMP = float.Parse(((DataRowView)ibsDetail.Current)["QT_QTY_TEMP"].ToString());
                        if (QT_QTY > QT_QTY_TEMP)
                        {
                            System.Windows.Forms.MessageBox.Show("請購數量超過訂購數量");
                            ((DataRowView)ibsDetail.Current)["QT_QTY"] = ((DataRowView)ibsDetail.Current)["QT_QTY_TEMP"].ToString();
                        }
                        //計算實售金額＝數量*售價
                        AM_AMT = float.Parse(((DataRowView)ibsDetail.Current)["PR_SALES"].ToString());
                        AM_AMT = AM_AMT * float.Parse(((DataRowView)ibsDetail.Current)["QT_QTY"].ToString());
                        ((DataRowView)ibsDetail.Current)["AM_SALES"] = AM_AMT.ToString();
                        ibsDetail.EndEdit();                       
                    }
                //重算總金額
                    InfoTextBox03.Text = infoDataGridView1.DoSum(8, totalMode.sum);
            }
        }

        public bool CheckRow(string value)
        {
            //使用DefaultVaildate的Vaildate屬性,檢查明細是否空白
            if (ibsDetail.List.Count == 0 && InfoRefValBox0.TextBoxText!="")
            { return false; }
            else
            { return true; }

        }

        private void bindingNavigatorEditItem_Click(object sender, EventArgs e)
        {
            //以string儲存,在按下修改時
            ((DataRowView)ibsMaster.Current)["UPDATE_USER"] = CliUtils.fLoginUser;
            ((DataRowView)ibsMaster.Current)["UPDATE_DATE"] = string.Format("{0:yyyyMMdd}", DateTime.Now.Date);
            ((DataRowView)ibsMaster.Current)["UPDATE_TIME"] = string.Format("{0:hhmmss}", DateTime.Now);

        }

        private void ibsMaster_AfterEdit(object sender, EventArgs e)
        {
            //以string儲存
            ((DataRowView)ibsMaster.Current)["UPDATE_USER"] = CliUtils.fLoginUser;
            ((DataRowView)ibsMaster.Current)["UPDATE_DATE"] = string.Format("{0:yyyyMMdd}", DateTime.Now.Date);
            ((DataRowView)ibsMaster.Current)["UPDATE_TIME"] = string.Format("{0:hhmmss}", DateTime.Now);
        }

        private void infoDataGridView2_Click(object sender, EventArgs e)
        {
            //重新設定明細欄位的ReadOnly屬性(因為使用AutoDisableControl會造成回歸預設值False)  
            this.infoDataGridView2.ReadOnly = true;
        }

        private void infoDataGridView1_Click(object sender, EventArgs e)
        {
            //重新設定明細欄位的ReadOnly屬性(因為使用AutoDisableControl會造成回歸預設值False) 
            this.nOPRDataGridViewTextBoxColumn1.ReadOnly = true;
            this.CN_SEQ2.ReadOnly = true;
            this.nMITEMDataGridViewTextBoxColumn.ReadOnly = true;
            this.cDIMUMDataGridViewTextBoxColumn.ReadOnly = true;
            this.pRSALESDataGridViewTextBoxColumn.ReadOnly = true;
            this.aMSALESDataGridViewTextBoxColumn.ReadOnly = true;       
            this.qTPOENDDataGridViewTextBoxColumn.ReadOnly = true;
            this.FG_COUNT.ReadOnly = true;
            this.fGCLOSEDataGridViewTextBoxColumn.ReadOnly = true;
            this.ID_PUR.ReadOnly = true;           
        }

        private void infoNavigator1_StateChanged(object sender, InfoNavigatorStateChangedEventArgs e)
        {
            //如果為一般性選購單,不可變更INQ,案源性質,客戶編號,業務員
            if (InfoComboBox0.Text == "一般請購單" || InfoComboBox0.Text == "1")
            {
                InfoRefValBox0.Enabled = true;
                InfoRefValBox02.Enabled = false;
                InfoComboBox01.Enabled = false;
                InfoRefValBox03.Enabled = false;
                InfoRefValBox01.Enabled = false;
                this.nOITEMDataGridViewTextBoxColumn.ReadOnly = true;
                this.nOITEMDataGridViewTextBoxColumn.DefaultCellStyle.BackColor = System.Drawing.Color.LightGray;
            }
            else if (InfoComboBox0.Text == "計劃性請購單" || InfoComboBox0.Text == "2")
            {
                InfoRefValBox0.Enabled = false;
                InfoRefValBox02.Enabled = true;
                InfoComboBox01.Enabled = true;
                InfoRefValBox03.Enabled = true;
                InfoRefValBox01.Enabled = true;
                InfoRefValBox0.TextBoxText = "";
                infoDataGridView1.AllowUserToAddRows = true;
                this.nOITEMDataGridViewTextBoxColumn.ReadOnly=false;
                this.nOITEMDataGridViewTextBoxColumn.DefaultCellStyle.BackColor = System.Drawing.Color.White;            
                
            }

            //將DateTimePicker設為不可改
            this.InfoDateTimePicker01.Enabled = false;
            this.InfoDateTimePicker1.Enabled = false;
            //請購類別不給改
            this.InfoComboBox0.Enabled = false;

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //隱藏鍵值欄位            
            this.nOPRDataGridViewTextBoxColumn1.Visible = false;
        }

        private void ibsDetail_BeforeEdit(object sender, EventArgs e)
        {
            //記錄Detail的修改時間日期
            ((DataRowView)ibsDetail.Current)["UPDATE_USER"] = CliUtils.fLoginUser;
            ((DataRowView)ibsDetail.Current)["UPDATE_DATE"] = string.Format("{0:yyyyMMdd}", DateTime.Now.Date);
            ((DataRowView)ibsDetail.Current)["UPDATE_TIME"] = string.Format("{0:hhmmss}", DateTime.Now);
        }

        private void infoDataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            //重算總金額
            InfoTextBox03.Text = infoDataGridView1.DoSum(8, totalMode.sum);
        }

        private void bindingNavigatorAddNewItem_MouseUp(object sender, MouseEventArgs e)
        {
            //將DateTimePicker設為不可改
            this.InfoDateTimePicker01.Enabled = false;
            this.InfoDateTimePicker1.Enabled = false;
        }
    }
    
}

